import 'package:flutter/material.dart';

/// 带动画效果的点击组件
class AnimatedTapWidget extends StatefulWidget {
  final Widget child;
  final VoidCallback? onTap;
  final double scaleDownValue;
  final Duration animationDuration;
  final Curve curve;

  const AnimatedTapWidget({
    super.key,
    required this.child,
    this.onTap,
    this.scaleDownValue = 0.95,
    this.animationDuration = const Duration(milliseconds: 150),
    this.curve = Curves.easeInOut,
  });

  @override
  State<AnimatedTapWidget> createState() => _AnimatedTapWidgetState();
}

class _AnimatedTapWidgetState extends State<AnimatedTapWidget>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _scaleAnimation;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      duration: widget.animationDuration,
      vsync: this,
    );
    _scaleAnimation = Tween<double>(
      begin: 1.0,
      end: widget.scaleDownValue,
    ).animate(
      CurvedAnimation(parent: _animationController, curve: widget.curve),
    );
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  void _handleTap() async {
    // 开始缩小动画
    await _animationController.forward();
    // 回弹到原大小
    await _animationController.reverse();

    // 执行点击回调
    widget.onTap?.call();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _handleTap,
      child: AnimatedBuilder(
        animation: _scaleAnimation,
        builder: (context, child) {
          return Transform.scale(
            scale: _scaleAnimation.value,
            child: widget.child,
          );
        },
      ),
    );
  }
}

/// 带动画效果的按钮组件
class AnimatedTapButton extends StatefulWidget {
  final Widget child;
  final VoidCallback? onTap;
  final EdgeInsetsGeometry? padding;
  final Decoration? decoration;
  final double scaleDownValue;
  final Duration animationDuration;
  final Curve curve;

  const AnimatedTapButton({
    super.key,
    required this.child,
    this.onTap,
    this.padding,
    this.decoration,
    this.scaleDownValue = 0.95,
    this.animationDuration = const Duration(milliseconds: 150),
    this.curve = Curves.easeInOut,
  });

  @override
  State<AnimatedTapButton> createState() => _AnimatedTapButtonState();
}

class _AnimatedTapButtonState extends State<AnimatedTapButton>
    with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  late Animation<double> _scaleAnimation;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      duration: widget.animationDuration,
      vsync: this,
    );
    _scaleAnimation = Tween<double>(
      begin: 1.0,
      end: widget.scaleDownValue,
    ).animate(
      CurvedAnimation(parent: _animationController, curve: widget.curve),
    );
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  void _handleTap() async {
    // 开始缩小动画
    await _animationController.forward();
    // 回弹到原大小
    await _animationController.reverse();

    // 执行点击回调
    widget.onTap?.call();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _handleTap,
      child: AnimatedBuilder(
        animation: _scaleAnimation,
        builder: (context, child) {
          return Transform.scale(
            scale: _scaleAnimation.value,
            child: Container(
              padding: widget.padding,
              decoration: widget.decoration,
              child: widget.child,
            ),
          );
        },
      ),
    );
  }
}
